package com.pyue.eam.regi.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyue.eam.admin.api.entity.regi.ClientLibEntity;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.log.annotation.SysLog;
import com.pyue.eam.regi.service.ClientLibService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户表
 *
 * @author lucc
 * @date 2023-10-12 20:38:12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/clientLib")
@Tag(description = "clientLib", name = "客户表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ClientLibController {
    private final ClientLibService clientLibService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param clientLib 客户表
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    // @PreAuthorize("@pms.hasPermission('eam_clientLib_view')" )
    public R getClientLibPage(@ParameterObject Page page, @ParameterObject ClientLibEntity clientLib) {
        LambdaQueryWrapper<ClientLibEntity> wrapper = Wrappers.lambdaQuery(clientLib).orderByDesc(ClientLibEntity::getCreateTime);
        return R.okPage(clientLibService.page(page, wrapper));
    }

    /**
     * 所有客户信息
     */
    @Operation(summary = "所有客户信息", description = "所有客户信息")
    @GetMapping("/getClientData")
    public R<List<ClientLibEntity>> getClientData() {
        return R.okList(clientLibService.dataList());
    }

    /**
     * 通过id查询客户表
     *
     * @param clientId id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{clientId}")
    // @PreAuthorize("@pms.hasPermission('eam_clientLib_view')" )
    public R getById(@PathVariable("clientId") Long clientId) {
        return R.ok(clientLibService.getDataById(clientId));
    }

    /**
     * 新增客户表
     *
     * @param clientLib 客户表
     * @return R
     */
    @Operation(summary = "新增客户表", description = "新增客户表")
    @SysLog("新增客户表")
    @PostMapping
    // @PreAuthorize("@pms.hasPermission('eam_clientLib_add')" )
    public R save(@RequestBody ClientLibEntity clientLib) {
        return R.ok(clientLibService.saveData(clientLib));
    }

    /**
     * 修改客户表
     *
     * @param clientLib 客户表
     * @return R
     */
    @Operation(summary = "修改客户表", description = "修改客户表")
    @SysLog("修改客户表")
    @PutMapping
    // @PreAuthorize("@pms.hasPermission('eam_clientLib_edit')" )
    public R updateById(@RequestBody ClientLibEntity clientLib) {
        return R.ok(clientLibService.updateDataById(clientLib));
    }

    /**
     * 通过id删除客户表
     *
     * @param ids clientId列表
     * @return R
     */
    @Operation(summary = "通过id删除客户表", description = "通过id删除客户表")
    @SysLog("通过id删除客户表")
    @DeleteMapping
    // @PreAuthorize("@pms.hasPermission('eam_clientLib_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(clientLibService.removeDataByIds(CollUtil.toList(ids)));
    }


}
